/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thinkbread;

/**
 *
 * @author zzn
 */
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.*;

/**
 * @author funny
 *
 * 用以下URL求城市的经纬度，把经纬度放入函数retrieveStaticImage运算
 * http://maps.google.com/maps/geo?q=shanghai&output=csv&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
 *
 * 用以下URL输出城市地图
 * http://maps.google.com/staticmap?center=31.2243531,121.4759159&format=png32&zoom=8&size=320x240&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
 */
public class MapCanvas extends GameCanvas implements Runnable {

    private LayerManager layerManager;
    private TiledLayer layerMap;
    private Sprite sprite;

    private GoogleMaps gMap = null;
    private String apiKey = "ABQIAAAAyEBvvvJHLRJ1N3_E59pYgxSVm0SpI78wlisH0U3WFDtO-CoHPBRDjn6mR9bgX6P7vWrGKzLil0AGZw";
//常州的经纬度
    private double lng = 30.276525;
    private double lat = 120.13391;
//放大的倍数
    private int zoom = 17;
//调整位置的经纬度
    double[] cs;

    public MapCanvas() {
        super(true);
        this.init();

    }
    public void init(){
        this.layerManager = new LayerManager();

        gMap = new GoogleMaps(apiKey);
        cs = new double[]{lng, lat};
        Image image;
        try {
            Graphics g = getGraphics();
            image = gMap.retrieveStaticImage(this.getWidth(), this.getHeight(), lng, lat, zoom, "jpg");
            //image = Image.createImage("/topview_tiles.png");


            this.layerMap = new TiledLayer(1,1,image, image.getWidth(), image.getHeight());
            layerMap.setCell(0, 0, 1);
            //layerMap.setStaticTileSet(image, image.getWidth(), image.getHeight());
            System.out.println("****"+image.getWidth()+image.getHeight());
            this.layerManager.append(layerMap);
            this.layerManager.paint(g, 0, 0);
            flushGraphics(0, 0, this.getWidth(), this.getHeight());
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

//    protected void paint(Graphics g) {
//        try {
//            Image image = gMap.retrieveStaticImage(this.getWidth(), this.getHeight(), lng, lat, zoom, "jpg");
//            g.drawImage(image, 0, 0, g.TOP | g.LEFT);
//        } catch (Exception e) {
//            //e.printStackTrace();
//        }
//    }

    protected void keyPressed(int keycode) {
        /*System.out.println(keycode);
        flag=true;*/
        //switch (this.getGameAction(keycode)) {
        String actionName = this.getKeyName(keycode);
        int action = this.getGameAction(keycode);
        if (action == GameCanvas.UP) {
            //lat=MapUtil.adjust(lat, lng, 0, -30, zoom)[0];
            //lng=MapUtil.adjust(lat, lng, 0, -30, zoom)[1];
        } else if (action == GameCanvas.DOWN) {
            //lat=MapUtil.adjust(lat, lng, 0, 30, zoom)[0];
            //lng=MapUtil.adjust(lat, lng, 0, 30, zoom)[1];
        } else if (action == GameCanvas.LEFT) {
            //lat=MapUtil.adjust(lat, lng, -30, 0, zoom)[0];
            //lng=MapUtil.adjust(lat, lng, -30, 0, zoom)[1];
        } else if (action == GameCanvas.RIGHT) {
            //lat=MapUtil.adjust(lat, lng, 30, 0, zoom)[0];
            //lng=MapUtil.adjust(lat, lng, 30, 0, zoom)[1];
        } else if (actionName.equals("1")) {
            if (zoom < 17) {
                zoom++;
            }
        } else if (actionName.equals("3")) {
            if (zoom > 1) {
                zoom--;
            }
        }
        //lat=cs[0];
        //lng=cs[1];
        //cs=MapUtil.adjust(lat, lng, 30, 0, zoom--);
        repaint();
    }

    public void run() {
    }
}


